/*
 * Created on Jun 10, 2010
 */
package ro.letsdoitromania.forms;

import java.util.Vector;
import javax.microedition.lcdui.*;
import com.mgmaps.app.APPCommands;
import com.mgmaps.app.PendingForm;
import com.mgmaps.tools.UITools;
import com.mgmaps.utils.AsyncCalls;
import com.mgmaps.utils.LS;
import com.mgmaps.utils.SMSSender;
import ro.letsdoitromania.Commands;
import ro.letsdoitromania.LDIRCanvas;
import ro.letsdoitromania.LDIRMidlet;
import ro.letsdoitromania.Settings;

public class ShareForm implements CommandListener {
  private Form form;
  
  private StringItem siError;
  private StringItem siInfo;
  private TextField tfNum;
  private TextField tfMsg;

  private static ShareForm instance;
  private String language;
  
  public static ShareForm getInstance() {
    if (instance == null)
      instance = new ShareForm();
    return instance;
  }
  
  private ShareForm() {
    language = Settings.language;
  }
  
  private void resetForm() {
    siError = null;
    siInfo = null;
    tfNum = null;
    tfMsg = null;
  }
  
  public void show() {
    show(null);
  }
  
  public void show(String errmsg) {
    if (language != Settings.language)
      resetForm();
    
    if (errmsg != null) {
      if (siError == null)
        siError = new StringItem(LS.get("app.form.error"), errmsg);
      else
        siError.setText(errmsg);
    }
    
    if (siInfo == null)
      siInfo = new StringItem("", LS.get("ldir.share.info"));
    
    if (tfNum == null)
      tfNum = new TextField(LS.get("ldir.share.phonenum"), "", 30, TextField.PHONENUMBER);
    tfNum.setInitialInputMode("IS_LATIN_DIGITS");
    tfNum.setString("");
    
    if (tfMsg == null)
      tfMsg = new TextField(LS.get("ldir.share.msgtitle"), "", 160, TextField.ANY);
    tfMsg.setString(LS.get("ldir.share.msg"));

    Vector itemVector = new Vector();
    if (errmsg != null)
      itemVector.addElement(siError);
    itemVector.addElement(siInfo);
    itemVector.addElement(tfNum);
    itemVector.addElement(tfMsg);
    
    final Item[] items = new Item[itemVector.size()];
    itemVector.copyInto(items);
    itemVector.removeAllElements();
    
    form = UITools.buildForm(form, LS.get("ldir.share.title"), items,
        new Command[] { Commands.cinstance.sendCommand, APPCommands.instance.backCommand }, this);
    
    Display.getDisplay(LDIRMidlet.instance).setCurrent(form);    
  }

  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == Commands.cinstance.sendCommand) {
      final String phoneNo = tfNum.getString().trim();
      final String msgData = tfMsg.getString().trim();
      
      PendingForm.instance.show(form, LS.get("ldir.share.title"), LS.get("ldir.share.sending")); 
      
      AsyncCalls.instance.setRunnable(new Runnable() {
        public void run() {
          try {
            new SMSSender().sendMessage(phoneNo, msgData);
            Display.getDisplay(LDIRMidlet.instance).setCurrent(LDIRCanvas.instance);
          }
          catch (Exception ex) {
            ShareForm.getInstance().show(LS.get("ldir.share.errorsending"));
          }
        }
      });
    }
    
    else {
      Display.getDisplay(LDIRMidlet.instance).setCurrent(LDIRCanvas.instance);
    }
  }
}
